package com.ruoyi.web.controller.api;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.api.UUser;
import com.ruoyi.system.service.IUUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.api.WRecordTransferOut;
import com.ruoyi.system.service.IWRecordTransferOutService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 链上提现Controller
 * 
 * @author ruoyi
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/api/wRecordTransferOut")
public class WRecordTransferOutController extends BaseController
{
    @Autowired
    private IWRecordTransferOutService wRecordTransferOutService;

    @Autowired
    private IUUserService userService;

    /**
     * 查询链上提现列表
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordTransferOut:list')")
    @GetMapping("/list")
    public TableDataInfo list(WRecordTransferOut wRecordTransferOut)
    {
        if (!StringUtils.isEmpty(wRecordTransferOut.getUserEmail())){
            UUser user = new UUser();
            user.setEmail(wRecordTransferOut.getUserEmail());

            List<UUser> users = userService.selectUUserList(user);

            if (!users.isEmpty()){
                wRecordTransferOut.setUserId(users.get(0).getId());
            }else {
                wRecordTransferOut.setUserId(1l);
            }
        }
        startPage();
        List<WRecordTransferOut> list = wRecordTransferOutService.selectWRecordTransferOutList(wRecordTransferOut);
        if (!list.isEmpty()){
            for (WRecordTransferOut u: list) {
                UUser user = userService.selectUUserById(u.getUserId());

                if (user != null){
                    u.setUserEmail(user.getEmail());
                }

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出链上提现列表
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordTransferOut:export')")
    @Log(title = "链上提现", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WRecordTransferOut wRecordTransferOut)
    {
        List<WRecordTransferOut> list = wRecordTransferOutService.selectWRecordTransferOutList(wRecordTransferOut);
        ExcelUtil<WRecordTransferOut> util = new ExcelUtil<WRecordTransferOut>(WRecordTransferOut.class);
        util.exportExcel(response, list, "链上提现数据");
    }

    /**
     * 获取链上提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordTransferOut:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(wRecordTransferOutService.selectWRecordTransferOutById(id));
    }

    /**
     * 新增链上提现
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordTransferOut:add')")
    @Log(title = "链上提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WRecordTransferOut wRecordTransferOut)
    {
        return toAjax(wRecordTransferOutService.insertWRecordTransferOut(wRecordTransferOut));
    }

    /**
     * 修改链上提现
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordTransferOut:edit')")
    @Log(title = "链上提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WRecordTransferOut wRecordTransferOut)
    {
        return toAjax(wRecordTransferOutService.updateWRecordTransferOut(wRecordTransferOut));
    }

    /**
     * 删除链上提现
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordTransferOut:remove')")
    @Log(title = "链上提现", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wRecordTransferOutService.deleteWRecordTransferOutByIds(ids));
    }
}
